//
//  AlarmTypeStatisticsView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/27.
//

import Foundation
import UIKit
import AAInfographics

class AlarmTypeStatisticsView: UIScrollView {
    let summaryV = AlarmTypeSummaryView()
    let repairStatisV = UIView.createBase()
    let numChartView = AAChartView()

    let averageRepairStatisV = UIView.createBase()
    let dateChartView = AAChartView()
    
    let gradientColorDic1 = AAGradientColor.linearGradient(
        direction: .toBottom,
        startColor: AARgba(230, 232, 238, 1),
        endColor: AARgba(230, 232, 238, 0.14)
    )
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reloadData(withModal modal: AlarmByTypeModal?) {
        summaryV.reloadData(withModal: modal)
        
        var sjqsDates: [String] = []
        var sjqsTotals: [Int64] = []

        if let sjqs = modal?.sjqs {
            sjqs.forEach { s in
                if let date = s.date {
                    sjqsDates.append(date)
                }
                
                if let total = s.total {
                    sjqsTotals.append(total)
                }
            }
        }
        
        let numChartModel = AAChartModel()
            .chartType(.area)
            .categories(sjqsDates)
            .xAxisVisible(true)
            .markerRadius(4)
            .markerSymbolStyle(.innerBlank)
            .markerSymbol(.circle)
            .legendEnabled(false)
            .dataLabelsEnabled(false)
            .series([
                AASeriesElement()
                    .name("")
                    .lineWidth(2)
                    .color("#3C72FF")
                    .fillColor(gradientColorDic1)
                    .data(sjqsTotals),
                ])

        numChartView.aa_refreshChartWholeContentWithChartModel(numChartModel)
        
        var sjfbDates: [String] = []
        var sjfbTotals: [Int64] = []

        if let sjqs = modal?.sjfb {
            sjqs.forEach { s in
                if let date = s.date {
                    sjfbDates.append(date)
                }
                
                if let total = s.total {
                    sjfbTotals.append(total)
                }
            }
        }
        
        let dateChartModel = AAChartModel()
            .chartType(.column)
            .categories(sjfbDates)
            .xAxisVisible(true)
            .markerRadius(4)
            .markerSymbolStyle(.innerBlank)
            .markerSymbol(.circle)
            .legendEnabled(false)
            .dataLabelsEnabled(false)
            .series([
                AASeriesElement()
                    .name("")
                    .lineWidth(2)
                    .color("#3C72FF")
                    .data(sjfbTotals),
                ])

        dateChartView.aa_refreshChartWholeContentWithChartModel(dateChartModel)
    }
    
    // MARK: - Setup
    func setupRepairStatisView() {
        addSubview(repairStatisV)
        repairStatisV.snp.makeConstraints { make in
            make.top.equalTo(summaryV.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(ScreenWidth - 28)
        }
        
        let titleBT = RRDurationTitleView(withIcon: "ic_rr_alarm_1", title: "数量趋势")
        repairStatisV.addSubview(titleBT)
        titleBT.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
        numChartView.frame = CGRect(x: 13, y: 50, width: ScreenWidth - 26, height: 140)
        repairStatisV.addSubview(numChartView)
        numChartView.snp.makeConstraints { make in
            make.top.equalTo(titleBT.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(140)
            make.bottom.equalToSuperview().offset(-16)
        }

        let aaChartModel = AAChartModel()
            .chartType(.area)
            .categories([])
            .xAxisVisible(true)
            .markerRadius(4)
            .markerSymbolStyle(.innerBlank)
            .markerSymbol(.circle)
            .legendEnabled(false)
            .dataLabelsEnabled(false)
            .series([
                AASeriesElement()
                    .name("")
                    .lineWidth(2)
                    .color("#3C72FF")
                    .fillColor(gradientColorDic1)
                    .data([]),
                ])

        numChartView.aa_drawChartWithChartModel(aaChartModel)
    }
    
    func setupAverageRepairStatisView() {
        addSubview(averageRepairStatisV)
        averageRepairStatisV.snp.makeConstraints { make in
            make.top.equalTo(repairStatisV.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(ScreenWidth - 28)
            make.bottom.equalToSuperview().offset(-33)
        }
        
        let titleBT = RRDurationTitleView(withIcon: "ic_rr_alarm_2", title: "时间分布")
        averageRepairStatisV.addSubview(titleBT)
        titleBT.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
        dateChartView.frame = CGRect(x: 13, y: 50, width: ScreenWidth - 26, height: 230)
        averageRepairStatisV.addSubview(dateChartView)
        dateChartView.snp.makeConstraints { make in
            make.top.equalTo(titleBT.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(230)
            make.bottom.equalToSuperview().offset(-20)
        }

        let aaChartModel = AAChartModel()
            .chartType(.column)
            .categories([])
            .xAxisVisible(true)
            .markerRadius(4)
            .markerSymbolStyle(.innerBlank)
            .markerSymbol(.circle)
            .legendEnabled(false)
            .dataLabelsEnabled(false)
            .series([
                AASeriesElement()
                    .name("")
                    .lineWidth(2)
                    .color("#3C72FF")
                    .data([]),
                ])

        dateChartView.aa_drawChartWithChartModel(aaChartModel)
    }
    
    func setupUI() {
        addSubview(summaryV)
        summaryV.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(ScreenWidth - 28)
            make.height.equalTo(181)
        }
        
        setupRepairStatisView()
        
        setupAverageRepairStatisView()
    }
}

class AlarmTypeSummaryView: UIView {
    
    let totalItem = AlarmTypeSummaryItem(withIsSmall: false)
    let dbjsItem = AlarmTypeSummaryItem()
//    let pjxysjItem = AlarmTypeSummaryItem()
    let pjczsjItem = AlarmTypeSummaryItem()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reloadData(withModal modal: AlarmByTypeModal?) {
        self.totalItem.updateUI(withKey: "本年总数（起）", value: "\(modal?.yjTotal ?? 0)")
//        self.dbjsItem.updateUI(withKey: "对比减少", value: "\(modal?.hbRatio ?? "0")%")
        self.dbjsItem.updateUI(withKey: "环比", value: "\(modal?.hbRatio ?? "0")%")
//        self.pjxysjItem.updateUI(withKey: "平均响应时间", value: "0分钟")
        self.pjczsjItem.updateUI(withKey: "平均处置时间", value: "\(modal?.avgCz ?? "0")分钟")
    }
   
    func setupUI() {
        let baseV = UIView.createBase()
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        totalItem.updateUI(withKey: "本年总数（起）", value: "0")
        baseV.addSubview(totalItem)
        totalItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(19)
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(50)
        }
        
//        let width = (ScreenWidth - 36) / 3.0
//
//        baseV.addSubview(dbjsItem)
//        dbjsItem.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(19)
//            make.top.equalTo(totalItem.snp.bottom).offset(33)
//            make.height.equalTo(40)
//            make.width.equalTo(width)
//        }
//
//        baseV.addSubview(pjxysjItem)
//        pjxysjItem.snp.makeConstraints { make in
//            make.left.equalTo(dbjsItem.snp.right)
//            make.centerY.equalTo(dbjsItem.snp.centerY)
//            make.height.equalTo(40)
//            make.width.equalTo(width)
//        }
//
//        baseV.addSubview(pjczsjItem)
//        pjczsjItem.snp.makeConstraints { make in
//            make.left.equalTo(pjxysjItem.snp.right)
//            make.centerY.equalTo(dbjsItem.snp.centerY)
//            make.height.equalTo(40)
//            make.width.equalTo(width)
//        }
        
        let width = (ScreenWidth - 36) / 2.0
        
        dbjsItem.updateUI(withKey: "环比", value: "0%")
        baseV.addSubview(dbjsItem)
        dbjsItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(19)
            make.top.equalTo(totalItem.snp.bottom).offset(33)
            make.height.equalTo(40)
            make.width.equalTo(width)
        }
        
        pjczsjItem.updateUI(withKey: "平均处置时间", value: "0分钟")
        baseV.addSubview(pjczsjItem)
        pjczsjItem.snp.makeConstraints { make in
            make.left.equalTo(dbjsItem.snp.right)
            make.centerY.equalTo(dbjsItem.snp.centerY)
            make.height.equalTo(40)
            make.width.equalTo(width)
        }
    }
}

class AlarmTypeSummaryItem: UIView {

    init(withIsSmall isSmall: Bool? = true) {
        super.init(frame: .zero)
        setupUI(withIsSmall: isSmall)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withKey key: String, value: String?) {
        keyL.text = key
        valL.text = value
    }
    
    func setupUI(withIsSmall isSmall: Bool? = true) {
        keyL.textColor = isSmall == true ? .hex("#999999") : .black
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left)
            make.top.equalTo(self.snp.top)
        }
        
        addSubview(valL)
        valL.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left)
            make.top.equalTo(keyL.snp.bottom).offset(10)
        }
        
        if isSmall == false {
            keyL.font = .systemFont(ofSize: 17)
            valL.font = .systemFont(ofSize: 26)
        }
    }
    
    lazy var keyL: UILabel = {
        let l = UILabel()
        l.textColor = .hex("#999999")
        l.font = UIFont.systemFont(ofSize: 14)
        l.textAlignment = .left
        return l
    }()
    
    lazy var valL: UILabel = {
        let l = UILabel()
        l.textColor = .hex("#3C72FF")
        l.font = UIFont.systemFont(ofSize: 17)
        l.textAlignment = .left
        return l
    }()
}
